cmake_minimum_required(VERSION 3.16)

project(VSKey VERSION 1.0.1)

set(SERVER_NAME vm_server)
set(SDK_NAME wm_key_1.0)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}")
string(REGEX MATCH "GNU" CMAKE_COMPILER_IS_GNU "${CMAKE_C_COMPILER_ID}")
string(REGEX MATCH "MSVC" CMAKE_COMPILER_IS_MSVC "${CMAKE_C_COMPILER_ID}")


aux_source_directory(MKey_server/code/main MAIN)
aux_source_directory(MKey_server/code/cmd CMD)
aux_source_directory(MKey_server/code/db_opr DB)
aux_source_directory(MKey_server/code/hard_enc CARD)
aux_source_directory(MKey_server/code/linklist LIST)
aux_source_directory(MKey_server/code/tcp TCP)
aux_source_directory(MKey_server/code/threshold SM2)
aux_source_directory(MKey_server/code/tools UTIL)

add_subdirectory(MKey_server/mbedtls)

add_executable(${SERVER_NAME} ${MAIN} ${CMD} ${DB} ${CARD} ${LIST} ${TCP} ${SM2} ${UTIL} )

target_include_directories(
        ${SERVER_NAME} PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}/MKey_server/code/include
        ${CMAKE_CURRENT_SOURCE_DIR}/MKey_server/code/hard_enc
        ${CMAKE_CURRENT_SOURCE_DIR}/MKey_server/code/linklist
        ${CMAKE_CURRENT_SOURCE_DIR}/MKey_server/mbedtls/include
)

target_link_libraries(
        ${SERVER_NAME} PUBLIC
        mbedtls
)

if(CMAKE_COMPILER_IS_MSVC)
    target_link_directories(${SERVER_NAME} PUBLIC
            ${CMAKE_CURRENT_SOURCE_DIR}/MKey_server/VS2013/lib/openssl/static/i386
    )
    target_link_libraries(
            ${SERVER_NAME} PUBLIC
            ws2_32
            libeay32
            ssleay32
    )
endif ()




aux_source_directory(MKey_client/code/middleware/apdu/apdu.algo MID_APDU_A)
aux_source_directory(MKey_client/code/middleware/apdu/apdu.core MID_APDU_C)
aux_source_directory(MKey_client/code/middleware/apdu/apdu.lib MID_APDU_L)
aux_source_directory(MKey_client/code/middleware/devmgr MID_DEV)
aux_source_directory(MKey_client/code/middleware/devmgr/virtual_dev MID_VIR)
aux_source_directory(MKey_client/code/middleware/k3gm/gm_api MID_GM)
aux_source_directory(MKey_client/code/middleware/k3gm/gm_data_mgr MID_GM_DATA)
aux_source_directory(MKey_client/code/middleware/mk_lib MID_LIB)
aux_source_directory(MKey_client/code/middleware/mtoken_gm MID_MT)
aux_source_directory(MKey_client/code/middleware/os/mkey MID_MKEY)
aux_source_directory(MKey_client/code/middleware/polarssl MID_POL)

aux_source_directory(MKey_client/code/mkey/apdu KEY_APDU)
aux_source_directory(MKey_client/code/mkey/cert KEY_CERT)
aux_source_directory(MKey_client/code/mkey/cmd KEY_CMD)
aux_source_directory(MKey_client/code/mkey/conf KEY_CONF)
aux_source_directory(MKey_client/code/mkey/mFS KEY_FS)
aux_source_directory(MKey_client/code/mkey/mHAL/alg/alg KEY_ALG)
aux_source_directory(MKey_client/code/mkey/mHAL/alg/alg_pr KEY_ALG_PR)
aux_source_directory(MKey_client/code/mkey/mHAL/drv KEY_DRV)
aux_source_directory(MKey_client/code/mkey/mHAL/hal KEY_HAL)

aux_source_directory(MKey_client/code/mkey/sys KEY_SYS)
aux_source_directory(MKey_client/code/mkey/Utils KEY_UTILS)

aux_source_directory(MKey_client/code/mkey/mHAL/include/mbedtls KEY_TLS)
aux_source_directory(MKey_client/code/mkey/mHAL/include/mbedtls/Threshold KEY_TLS_EX)


if(CMAKE_COMPILER_IS_MSVC)
    add_library(${SDK_NAME} SHARED ${MID_APDU_A} ${MID_APDU_C} ${MID_APDU_L} ${MID_DEV} ${MID_VIR} ${MID_GM} ${MID_GM_DATA} ${MID_LIB} ${MID_MT} ${MID_MKEY} ${MID_POL}
            ${KEY_APDU} ${KEY_CERT} ${KEY_CMD} ${KEY_CONF} ${KEY_FS} ${KEY_ALG} ${KEY_ALG_PR} ${KEY_DRV} ${KEY_HAL}  ${KEY_SYS} ${KEY_UTILS} ${KEY_TLS} ${KEY_TLS_EX}
            ${CMAKE_CURRENT_SOURCE_DIR}/MKey_client/client.def)
    set_property(TARGET ${SDK_NAME} PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") # -MT/-MTd
else ()
    #target_link_options(${SDK_NAME} PRIVATE "LINKER:--retain-symbols-file=${CMAKE_CURRENT_SOURCE_DIR}/src/def/kk.sym")
    #target_compile_options(${SDK_NAME} PRIVATE -fPIC)
    #strip
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s")
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s")
    #set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -D_POSIX_C_SOURCE=199309L")
endif()
target_include_directories(
        ${SDK_NAME} PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}/MKey_client/code
        ${CMAKE_CURRENT_SOURCE_DIR}/MKey_client/code/middleware
        ${CMAKE_CURRENT_SOURCE_DIR}/MKey_client/code/mkey
        ${CMAKE_CURRENT_SOURCE_DIR}/MKey_client/code/mkey/mHAL/include
        ${CMAKE_CURRENT_SOURCE_DIR}/MKey_client/code/mkey/mHAL/include/mbedtls
        ${CMAKE_CURRENT_SOURCE_DIR}/MKey_client/code/mkey/mHAL/include/mbedtls/Threshold/include
        ${CMAKE_CURRENT_SOURCE_DIR}/MKey_client/code/middleware/gm_api
        ${CMAKE_CURRENT_SOURCE_DIR}/MKey_client/code/mkey/sys
)


if(CMAKE_COMPILER_IS_MSVC)
    target_link_directories(${SDK_NAME} PUBLIC
            ${CMAKE_CURRENT_SOURCE_DIR}/MKey_server/VS2013/lib/openssl/static/i386
            )
    target_link_libraries(
            ${SDK_NAME} PUBLIC
            ws2_32
            libeay32
            ssleay32
    )
endif ()